<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          maxlength="11"
          show-word-limit
          clearable
          style="width: 220px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="出生日期" prop="birthDate">
        <el-date-picker
          clearable
          v-model="queryParams.birthDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择出生日期"
        >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          maxlength="10"
          show-word-limit
          style="width: 220px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班组" prop="workTeamName">
        <el-input
          v-model="queryParams.workTeamName"
          placeholder="请输入班组"
          clearable
          maxlength="10"
          style="width: 220px"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <div class="searchReset">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </div>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['personnel:personnel:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['personnel:personnel:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['personnel:personnel:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['personnel:personnel:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div :class="eleTabTop <= 85 ? 'eleTab' : ''" class="informationTab" ref="eleTab">
      <el-table
        v-loading="loading"
        :data="personnelList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" width="100" />
        <el-table-column
          label="姓名"
          prop="userName"
          min-width="150"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="手机号码"
          prop="phone"
          align="center"
          min-width="150"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="性别"
          align="center"
          prop="sex"
          min-width="100"
          :show-overflow-tooltip="true"
          :formatter="sexFormat"
        />
        <!-- <el-table-column label="出生日期" align="center" prop="birthDate" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column> -->
        <el-table-column label="应急撤号权" align="center" key="status" min-width="150">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.remove"
              active-value="1"
              inactive-value="0"
              @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="所属班组"
          prop="workTeamName"
          min-width="150"
          align="center"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="备注"
          prop="remark"
          min-width="200"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          fixed="right"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['personnel:personnel:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              class="delBtn_R"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['personnel:personnel:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改人员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="姓名" prop="userName">
              <el-input
                type="text"
                maxlength="10"
                show-word-limit
                placeholder="请输入姓名"
                v-model="form.userName"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phone">
              <el-input
                v-model="form.phone"
                placeholder="请输入手机号码"
                maxlength="11"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <!-- <el-col :span="12">
            <el-form-item label="出生日期" prop="birthDate">
              <el-date-picker
                style="width: 295px"
                clearable
                v-model="form.birthDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择出生日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="用户性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别" style="width: 295px">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属班组" prop="workTeamId">
              <el-select
                filterable
                v-model="form.workTeamId"
                placeholder="所属班组"
                style="width: 295px"
              >
                <el-option
                  v-for="dict in workteam"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10"> </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="人脸" prop="imageUrl">
              <img id="myCanvas" width="200" height="200" />
              <el-button type="primary" @click="capbase64">采 集</el-button>
              <el-button type="primary" @click="start_preview">初始化</el-button>
              <!--  && title === '人员信息-修改' -->
              <img
                v-if="imageUrl"
                :src="imageUrl"
                width="200"
                height="200"
                style="float: left; margin-right: 10px; border-radius: 50%"
              />
            </el-form-item>
          </el-col>
        </el-row>
         <el-row > <el-col>
           <el-form-item label="" prop="phone">
             <div style="color:red;font-size:16px">请脱帽露出五官，正视摄像头</div>
            </el-form-item>
           </el-col> </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                placeholder="请输入备注"
                type="textarea"
                maxlength="100"
                show-word-limit
                :rows="10"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPersonnel,
  getPersonnel,
  delPersonnel,
  addPersonnel,
  updatePersonnel,
  workTeams,
  removePersonnel,
} from "@/api/peopleManage/peopelManage";
import _ from "lodash";
export default {
  name: "Personnel",
  dicts: ["sys_normal_disable", "sys_user_sex"],
  data() {
    return {
      basefaceData: "",
      eleTabTop: 0, //表格容器eleTabTop距浏览器顶部距离
      workteam: [],
      //文件列表
      fileList: [],
      files: {},
      imageUrl: "",
      MixinUploadAllApi: process.env.VUE_APP_BASE_API + "/file/upload", //上传图片路径
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人员信息表格数据
      personnelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        sex: null,
        phone: null,
        birthDate: null,
        workTeamId: null,
        faceData: null,
      },
      // 表单参数
      form: {},
      sexs: [],
      // 表单校验
      rules: {
        imageUrl: [{ required: true, message: "照片不能为空", trigger: "blur" }],
        userName: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        workTeamId: [{ required: true, message: "班组id不能为空", trigger: "blur" }],
        delFlag: [{ required: true, message: "删除标志不能为空", trigger: "blur" }],
        birthDate: [{ required: true, message: "出生日期不能为空", trigger: "blur" }],
        sex: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
      ws: "",
      g_bConnectWS: false,
      g_splitFlag: "$$@*@$$",
      g_bClosed: false, //是否被关闭了
      g_bOpen: false, //是否打开
      g_index: 1, //当前高拍仪设备索引
      num: 1,
      ImageBase64: "",
      flag:false
    };
  },
  watch: {
    open() {
      if (!this.open) {
        this.unload();
      }
      // this.stop_preview();
    },
    ImageBase64() {
      if (this.ImageBase64) {
        this.SetFocus(this);
        // base64转图片获取图片url
        const blobData = this.baseImg(`data:image/jpg;base64,${this.ImageBase64}`);
        const urld = window.URL.createObjectURL(new Blob([blobData]));
        console.log(urld);
        this.imageUrl = urld;
      }
    },
  },
  created() {
    this.getList();
    this.getDicts("sys_user_sex").then((response) => {
      this.sexs = response.data;
    });
  },
  mounted() {
    // 滚动条的获取
    this.load();
    this.handleScrollx();
    window.addEventListener("scroll", this.handleScrollx, true);
  },
  activated() {
    //获取表格容器距页面头部的距离
    this.handleScrollx();
  },
  destroyed() {
    // this.unload();
    //  this.stop_preview();
    window.removeEventListener("scroll", this.handleScrollx, true);
  },
  methods: {
    handleStatusChange(row) {
      let params = {};
      params.id = row.id;
      params.remove = row.remove;
      this.$modal
        .confirm('确认要修改状态吗？')
        .then(function () {
          return removePersonnel(params);
        })
        .then(() => {
          this.msgSuccess("修改成功")
           this.getList();
        })
        .catch(function () {
          row.remove = row.remove === "0" ? "1" : "0";
        });
    },
    capbase64() {
    if(this.form.id){
      this.flag = true;
    }
      this.cap2base64(0);
    },
    cap2base64(idx) {
      var jsonObj = { FuncName: "Cap2Base64", index: idx };
      this.sendWsMessage(jsonObj);
    },
     sendWsMessage(jsonObj) {
      var jsonStr = JSON.stringify(jsonObj);
      this.ws.send(jsonStr);
    },
    //开启webSocket
    StartWebSocket() {
      var url = "ws://localhost:27896";

      if ("WebSocket" in window) {
        this.ws = new WebSocket(url);
      } else if ("MozWebSocket" in window) {
        this.ws = new MozWebSocket(url);
      } else {
        alert(
          "浏览器版本过低，请升级您的浏览器。\r\n浏览器要求：IE10+/Chrome14+/FireFox7+/Opera11+"
        );
      }
      const _this = this;
      this.ws.onopen = function () {
        this.g_bConnectWS = true;
        _this.sendInitMsg(); //初始化
        this.g_bClosed = false;
      };

      this.ws.onmessage = function (evt) {
        if (typeof evt.data == "string") {
          var str = evt.data;
          if (str.length <= 0) {
            return;
          }
          var message = JSON.parse(str);
          // _this.output(str); //返回信息
          if ("VideoStream" == message["FuncName"]) {
            //视频帧
            _this.setImageWithBase64(message["ImageBase64"]);
          } else {
            _this.output(str); //返回信息
          }
          message = null;
          str = null;
        }
      };

      this.ws.onclose = function () {
        const _this = this;
        this.g_bConnectWS = false;
        _this.output("连接已断开或服务端未开启");
      };
    },



    load() {
      //连接服务
      this.StartWebSocket();
    },

    unload() {
      const _this = this;
      // _this.output("断开连接");
      if (_this.g_bClosed) {
        return;
      }
      clearInterval();
      //反初始化
      var jsonObj = { FuncName: "HWUnInitCamLib", index: _this.g_index };
      this.sendWsMessage(jsonObj);
    },
    //初始化
    sendInitMsg() {
      var jsonObj = { FuncName: "HWInitCamLib", index: this.g_index };
      this.sendWsMessage(jsonObj);
    },

    //显示视频帧
    setImageWithBase64(str) {
      var myimg = document.getElementById("myCanvas");
      myimg.src = "data:image/png;base64," + str;
    },
    //输出信息
    output(message) {
      let params = JSON.parse(message);
      if (params.FuncName === "Cap2Base64")
        this.ImageBase64 = params.RetInfo["ImageBase64"];
      var output = document.getElementById("output");
      // output.innerHTML = output.innerHTML + message + "\n";
      // output.scrollTop = output.scrollHeight;
    },

    //拍照
    Capture() {
      var filepath = document.getElementById("saveText").value;
      var jsonObj = { FuncName: "camCaptureImageFile", argument: { filePath: filepath } };
      this.sendWsMessage(jsonObj);
    },

    IdFaceCompare() {
      var jsonObj = { FuncName: "CompareFace", index: this.g_index };
      this.sendWsMessage(jsonObj);
    },

    change_preview() {
      if (0 == this.g_index) {
        this.stop_preview();
        this.g_index = 1;
        this.start_preview();
      } else {
        this.stop_preview();
        this.g_index = 0;
        this.start_preview();
      }
    },

    start_preview() {
      //var jsonObj = {FuncName:'SetPreviewQuality', flags:30};
      //this.sendWsMessage(jsonObj);
      var jsonObj = { FuncName: "StartPreview", index: this.g_index };
      this.sendWsMessage(jsonObj);
    },

    stop_preview() {
      var jsonObj = { FuncName: "StopPreview", index: this.g_index };
      this.sendWsMessage(jsonObj);
    },

    FormatNum(Source) {
      var strTemp = "";
      var len = 5;
      if (Source < 10) {
        len = 4;
      } else if (Source < 100) {
        len = 3;
      } else if (Source < 1000) {
        len = 2;
      }
      for (let i = 0; i < len; i++) {
        strTemp += "0";
      }
      return strTemp + Source;
    },

    capture() {
      // var val = this.num;
      // var filename = this.FormatNum(val);
      var ext = "bmp";
      var path = "D:\\Img" + 1 + "." + ext; //get_name()
      this.num++;
      console.log(path);
      var jsonObj = { FuncName: "DoCapture", index: this.g_index, filepath: path };
      this.sendWsMessage(jsonObj);
    },

    //纠偏裁边
    RotateCrop(obj) {
      if (obj.checked) {
        var jsonObj = { FuncName: "SetAutoCrop", index: this.g_index, flag: 1 };
        this.sendWsMessage(jsonObj);
      } else {
        var jsonObj = { FuncName: "SetAutoCrop", index: this.g_index, flag: 0 };
        this.sendWsMessage(jsonObj);
      }
    },

    CleanBlack(obj) {
      if (obj.checked) {
        var jsonObj = { FuncName: "CleanBorder", index: this.g_index, flag: 1 };
        this.sendWsMessage(jsonObj);
      } else {
        var jsonObj = { FuncName: "CleanBorder", index: this.g_index, flag: -1 };
        this.sendWsMessage(jsonObj);
      }
    },

    GetFocus(idx) {
      var jsonObj = { FuncName: "GetCamFocus", index: this.g_index };
      this.sendWsMessage(jsonObj);
    },

    SetFocus(obj) {
      if (obj.checked) {
        var jsonObj = {
          FuncName: "SetCamFocus",
          index: this.g_index,
          flag: 1,
          focusValue: 412,
        };
        this.sendWsMessage(jsonObj);
      } else {
        var jsonObj = {
          FuncName: "SetCamFocus",
          index: this.g_index,
          flag: -1,
          focusValue: 412,
        };
        this.sendWsMessage(jsonObj);
      }
    },

    readidcard() {
      var jsonObj = { FuncName: "ReadIDCard" };
      this.sendWsMessage(jsonObj);
    },

    setJpgQuality(idx, val) {
      var jsonObj = { FuncName: "SetJPGQuality", index: idx, flag: val };
      this.sendWsMessage(jsonObj);
    },

    cap2HttpServer(idx, url) {
      var jsonObj = { FuncName: "Cap2HttpServer", index: idx, URL: url, flag: 1 };
      this.sendWsMessage(jsonObj);
    },
    img2HttpServer(str, url) {
      var jsonObj = { FuncName: "Img2HttpServer", imgpath: str, URL: url, flag: 1 };
      this.sendWsMessage(jsonObj);
    },

    readBarcode() {
      var jsonObj = { FuncName: "ReadBarcode", index: this.g_index };
      this.sendWsMessage(jsonObj);
    },

    image2Base64(str) {
      var jsonObj = { FuncName: "Image2Base64", index: str, flag: 1 };
      this.sendWsMessage(jsonObj);
    },

    startAutoCap() {
      var jsonObj = { FuncName: "StartAutoCap", index: this.g_index };
      this.sendWsMessage(jsonObj);
    },
    stopAutoCap() {
      var jsonObj = { FuncName: "StopAutoCap", index: this.g_index };
      this.sendWsMessage(jsonObj);
    },

    setVideoColor(idx, val) {
      var jsonObj = { FuncName: "VideoColor", index: idx, flag: val };
      this.sendWsMessage(jsonObj);
    },

    cap2base64(idx) {
      var jsonObj = { FuncName: "Cap2Base64", index: idx };
      this.sendWsMessage(jsonObj);
    },

    //放大
    zoomin() {
      var jsonObj = { FuncName: "VideoZoom", index: this.g_index, flag: 1 };
      this.sendWsMessage(jsonObj);
    },

    //缩小
    zoomout() {
      var jsonObj = { FuncName: "VideoZoom", index: this.g_index, flag: -1 };
      this.sendWsMessage(jsonObj);
    },

    //左转
    rotleft() {
      var jsonObj = { FuncName: "VideoRotate", index: this.g_index, flag: 1 };
      this.sendWsMessage(jsonObj);
    },

    //左转
    rotright() {
      var jsonObj = { FuncName: "VideoRotate", index: this.g_index, flag: 2 };
      this.sendWsMessage(jsonObj);
    },

    //生成pdf
    createpdf(pdfPath, imgPath) {
      var jsonObj = {
        FuncName: "CreatePDF",
        pdfpath: pdfPath,
        imgpath: imgPath,
        flag: 1,
      };
      this.sendWsMessage(jsonObj);
    },

    mergeImage() {
      var jsonObj = {
        FuncName: "MergeImage",
        idCard: 1,
        DeleteSrcImg: 1,
        imgpath1: "D:\\IMG0000.jpg",
        imgpath2: "D:\\IMG0001.jpg",
        MergeImgPath: "D:\\1.jpg",
      };
      this.sendWsMessage(jsonObj);
    },
    SetCamRect() {
      var jsonObj = {
        FuncName: "HWSetImgRect",
        Top: 200,
        Left: 200,
        Right: 800,
        buttom: 800,
      };
      this.sendWsMessage(jsonObj);
    },

    GetCamInfo() {
      var jsonObj = { FuncName: "HWGetCamInfo" };
      this.sendWsMessage(jsonObj);
    },

    GetICCardInfo() {
      var jsonObj = { FuncName: "HWGetICCard" };
      this.sendWsMessage(jsonObj);
    },

    SetExposure() {
      var jsonObj = { FuncName: "HWSetExposure", ControlAuto: 0, ControlVal: -2 };
      this.sendWsMessage(jsonObj);
    },
    GetResloution() {
      var jsonObj = { FuncName: "HWGetResloution", index: this.g_index };
      this.sendWsMessage(jsonObj);
    },

    SetResloution() {
      var jsonObj = { FuncName: "HWSetResloution", index: this.g_index, ResIndex: 5 };
      this.sendWsMessage(jsonObj);
    },

    StartRecord() {
      var jsonObj = { FuncName: "HWStartRecord", ImagePath: "D:\\IMG0000.mp4" };
      this.sendWsMessage(jsonObj);
    },

    StopRecord() {
      var jsonObj = { FuncName: "HWStopRecord" };
      this.sendWsMessage(jsonObj);
    },

    //获取表格容器距页面头部的距离
    handleScrollx() {
      this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top;
    },
    sexFormat(row, column) {
      return this.selectDictLabelOld(this.sexs, row.sex);
    },
    /** 附件图片上传之前的校验 可公用*/
    beforeAvatarUpload(file) {
      console.log(file, "file");
      const isType =
        file.type === "image/jpeg" ||
        file.type === "image/jpg" ||
        file.type === "image/png" ||
        file.type === "image/bmp";
      const isLt1M = file.size / 1024 / 1024 < 1;
      if (!isType) {
        this.$message.error("上传图片只能是 JPG/JPEG/PNG 格式!");
      }

      return isType;
    },
    //上传成功方法
    handleSuccess(response, file, fileList) {
      if (response.code == 200) {
        //    this.fileList = fileList;
        // this.form.picpath = response.url;
      } else {
        this.msgError("上传图片失败");
      }
    },
    handleChange(file) {
      this.files = file;
    },
    // 移除图片方法
    handleRemove(file, fileList) {
      // this.fileList = [];
      // this.form.picpath = "";
      // this.$refs["form"].validateField("picpath");
    },
    /** 查询人员信息列表 */
    getList() {
      this.loading = true;
      listPersonnel(this.queryParams).then((response) => {
        this.personnelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userName: null,
        sex: null,
        phone: null,
        birthDate: null,
        workTeamId: null,
        faceData: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.imageUrl = "";
      workTeams().then((res) => {
        this.workteam = res.rows;
      });
      this.start_preview();
      this.SetFocus(this);
      this.open = true;
      this.title = "人员信息-添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.flag = false;
      this.reset();
      this.start_preview();
      this.SetFocus(this);
      // base64转图片获取图片url
      console.log(this.imageUrl);
      const id = row.id || this.ids;
      workTeams().then((res) => {
        this.workteam = res.rows;
      });
      getPersonnel(id).then((response) => {
        this.form = response.data;
        const blobData = this.baseImg(`data:image/jpg;base64,${response.data.faceData}`);
        // this.basefaceData = row.faceData;
        const urld = window.URL.createObjectURL(new Blob([blobData]));
        this.imageUrl = urld;
        this.open = true;
        this.title = "人员信息-修改";
      });
    },
    // base64转图片
    baseImg(dataurl) {
      if (!dataurl) return;
      var arr = dataurl.split(","),
        mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        n = bstr.length,
        u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
      }
      return new Blob([u8arr], { type: mime });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.imageUrl) {
        _.unset(this.rules, ["imageUrl"]);
      } else {
        this.rules.imageUrl = [
          { required: true, message: "拍照不能为空", trigger: "blur" },
        ];
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            // 图片转base64
            // var reader = new FileReader();
            // reader.readAsDataURL(this.files.raw); // 一定要传入file格式
            // reader.onload = () => {
              if(this.flag){
                 this.form.faceData = this.ImageBase64
              }
            updatePersonnel(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              // this.$refs.fil.clearFiles();
              this.open = false;
              this.getList();
            });
            // }
          } else {
            this.form.faceData = this.ImageBase64;
            addPersonnel(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
            // };
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除人员信息？")
        .then(function () {
          return delPersonnel(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "personnel/personnel/export",
        {
          ...this.queryParams,
        },
        `personnel_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
#myCanvas {
  border-radius: 50%;
}
</style>
